package k8.util;

import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

public class PixelStoreState
{
	public int unpackRowLength;
	public int unpackAlignment;
	public int unpackSkipRows;
	public int unpackSkipPixels;
	public int packRowLength;
	public int packAlignment;
	public int packSkipRows;
	public int packSkipPixels;

	private static IntBuffer scratch = BufferUtils.createIntBuffer(16);

	/**
	 * Constructor for PixelStoreState.
	 */
	public PixelStoreState()
	{
		load();
	}

	public void load()
	{
		unpackRowLength = glGetIntegerv(GL11.GL_UNPACK_ROW_LENGTH);
		unpackAlignment = glGetIntegerv(GL11.GL_UNPACK_ALIGNMENT);
		unpackSkipRows = glGetIntegerv(GL11.GL_UNPACK_SKIP_ROWS);
		unpackSkipPixels = glGetIntegerv(GL11.GL_UNPACK_SKIP_PIXELS);
		packRowLength = glGetIntegerv(GL11.GL_PACK_ROW_LENGTH);
		packAlignment = glGetIntegerv(GL11.GL_PACK_ALIGNMENT);
		packSkipRows = glGetIntegerv(GL11.GL_PACK_SKIP_ROWS);
		packSkipPixels = glGetIntegerv(GL11.GL_PACK_SKIP_PIXELS);
	}

	public void save()
	{
		GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, unpackRowLength);
		GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, unpackAlignment);
		GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, unpackSkipRows);
		GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, unpackSkipPixels);
		GL11.glPixelStorei(GL11.GL_PACK_ROW_LENGTH, packRowLength);
		GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, packAlignment);
		GL11.glPixelStorei(GL11.GL_PACK_SKIP_ROWS, packSkipRows);
		GL11.glPixelStorei(GL11.GL_PACK_SKIP_PIXELS, packSkipPixels);
	}

	/**
	 * Convenience method for returning an int.
	 * 
	 * @param what
	 * 
	 * @return int
	 */
	public static int glGetIntegerv(int what)
	{
		scratch.rewind();
		GL11.glGetInteger(what, scratch);
		return scratch.get();
	}
}
